package com.wodogs.recharge;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.wd.jdbc.AbstractService2;
import com.wd.model.LogJob;
import com.wodogs.recharge.entity.UserAccount;

@Component
public class BaseService extends AbstractService2 {
	
	public List<UserAccount> getUndoList(String platformName){
		if(StringUtils.isBlank(platformName)){
			return new ArrayList<>();
		}
		String sql = "select id,account,password,ecode,cardNo,cardPin,url,login_url from recharge where status=? and platform_name=? order by account";
		List<UserAccount> list = select(sql, Arrays.asList(0,platformName), UserAccount.class);
		
		if(list != null){
			for(UserAccount account:list){
				update(account.getId(), -1, "进行中");
			}
		}
		return list;
	}
	
	public void updateSuccess(Long id){
		update(id, 1,null);
	}
	
	public void updateFail(Long id,String msg){
		update(id, 4,msg);
	}
	
	public void updateAccountErr(Long id,String msg){
		update(id, 5,msg);
	}
	
	public void update(Long id,Integer status,String msg){
		if(id == null){
			return;
		}
		StringBuilder sb = new StringBuilder("update recharge set status=?,modifyDate=?");
		List<Object> para = new ArrayList<>();
		para.add(status);
		para.add(new Date());
		if(msg !=null){
			sb.append(",result=?");
			para.add(msg);
		}
		sb.append(" where id=?");
		para.add(id);
		executeUpdate(sb.toString(),para);
	}
	
	public void updateCaptchaPic(List<UserAccount> accountList,String captchaPic){
		if(accountList == null || accountList.size()<1){
			return;
		}
		StringBuilder sb = new StringBuilder("update recharge set captcha_pic=?");
		List<Object> para = new ArrayList<>();
		para.add(captchaPic);
		sb.append(" where id");
		if(accountList.size() == 1){
			sb.append("=?");
			para.add(accountList.get(0).getId());
		}else{
			sb.append(" in(?");
			para.add(accountList.get(0).getId());
			for(int i=1,j=accountList.size();i<j;i++){
				sb.append(",?");
				para.add(accountList.get(i).getId());
			}
			sb.append(")");
		}
		executeUpdate(sb.toString(),para);
	}
	
	public void update(List<UserAccount> accountList,Integer status,String msg){
		if(accountList == null || accountList.size()<1){
			return;
		}
		StringBuilder sb = new StringBuilder("update recharge set status=?,modifyDate=?");
		List<Object> para = new ArrayList<>();
		para.add(status);
		para.add(new Date());
		if(msg !=null){
			sb.append(",result=?");
			para.add(msg);
		}
		sb.append(" where id");
		if(accountList.size() == 1){
			sb.append("=?");
			para.add(accountList.get(0).getId());
		}else{
			sb.append(" in(?");
			para.add(accountList.get(0).getId());
			for(int i=1,j=accountList.size();i<j;i++){
				sb.append(",?");
				para.add(accountList.get(i).getId());
			}
			sb.append(")");
		}
		executeUpdate(sb.toString(),para);
	}
	
	public void insertData(LogJob logJob) {

		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = getConnection();
			String sql = "insert into app_log_job(type,createtime,remark) values(?,now(),?)";
			pst = con.prepareStatement(sql);
			pst.setString(1, logJob.getType());
			pst.setString(2, logJob.getRemark());
			pst.execute();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
//			CloseConnection(con);
		}
	}
}
